package com.gmlive.common.gmshare.share

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import com.gmlive.common.gmshare.activity.QQShareHandlerActivity
import com.gmlive.common.gmshare.base.IShareCallback
import com.gmlive.common.gmshare.base.IShareStrategy
import com.gmlive.common.gmshare.bean.ShareEntity
import com.gmlive.common.gmshare.interfaces.ShareConstant
import com.gmlive.common.gmshare.interfaces.ShareConstant.SHARE_CHANNEL_QQ
import com.gmlive.common.gmshare.interfaces.ShareConstant.SHARE_CHANNEL_QZONE
import com.gmlive.common.gmshare.interfaces.ShareConstant.SHARE_STATUS_FAILED
import com.gmlive.common.gmshare.util.ManifestUtil
import com.gmlive.common.gmshare.util.ShareUtil
import com.tencent.connect.share.QQShare
import com.tencent.connect.share.QzoneShare
import com.tencent.tauth.IUiListener
import com.tencent.tauth.Tencent
import com.tencent.tauth.UiError
import java.util.ArrayList

/**
 * Author: xs
 * Create on: 2020/06/17
 * Description: _QQ分享
 */
object QQShare : IShareStrategy {
    private var channel: Int = SHARE_CHANNEL_QQ
    private var mTencent: Tencent? = null
    private var shareEntity: ShareEntity? = null
    private var shareCallback: IShareCallback? = null

    private fun initQQApi(activity: Activity?) {
        mTencent = Tencent.createInstance(ManifestUtil.getTencentQQAppId(activity?.applicationContext), activity?.applicationContext)
    }

    override fun share(activity: Activity?, shareChannel: Int?, shareEntity: ShareEntity?, shareCallback: IShareCallback?) {
        this.channel = shareChannel ?: SHARE_CHANNEL_QQ
        this.shareEntity = shareEntity
        this.shareCallback = shareCallback
        if (null == shareEntity) {
            return
        }

        initQQApi(activity)

        activity?.let { QQShareHandlerActivity.start(it) }
    }

    fun shareWithQQOnActivity(activity: Activity?) {
        shareEntity?.let { data ->
            if (channel == SHARE_CHANNEL_QZONE) {
                val params = Bundle()
                params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT)
                params.putString(QzoneShare.SHARE_TO_QQ_TITLE, data.title) // 必填
                params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, data.content) // 选填
                params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, data.url) // 必填
                val arrayList = ArrayList<String?>()
                if (!data.imgUrl.isNullOrEmpty()) {
                    arrayList.add(data.imgUrl)
                }
                params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, arrayList)
                mTencent?.shareToQzone(activity, params, mCallBack)
            } else {
                if (data.isShareBigImg) {
                    if (!data.imgUrl.isNullOrEmpty() && data.imgUrl?.startsWith("http") == false) {
                        val params = Bundle()
                        params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, data.imgUrl) // 需要分享的本地图片URL
                        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE) // 设置分享类型为纯图片分享
                        params.putInt(QQShare.SHARE_TO_QQ_EXT_INT, QQShare.SHARE_TO_QQ_FLAG_QZONE_ITEM_HIDE) // 替它附加功能
                        mTencent?.shareToQQ(activity, params, mCallBack)
                    } else {
                        shareCallback?.failed(channel, SHARE_STATUS_FAILED, "大图分享不支持网络图片")
                    }
                } else {
                    if (!(data.url.isNullOrEmpty() || data.title.isNullOrEmpty())) {
                        val params = Bundle()
                        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT)
                        params.putString(QQShare.SHARE_TO_QQ_TITLE, data.title) // 标题 必填
                        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, data.content) // 摘要 必填
                        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, data.url) // 内容地址 必填
                        if (data.imgUrl?.isNotEmpty() == true) {
                            params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, data.imgUrl) // 网络图片地址
                        }
                        mTencent?.shareToQQ(activity, params, mCallBack)
                    } else {
                        var activityName = ""
                        val intentSend = Intent(Intent.ACTION_SEND)
                        intentSend.type = "text/plain"
                        val listActivity = activity?.packageManager?.queryIntentActivities(intentSend, 0)
                        if (listActivity != null) {
                            for (resolveInfo in listActivity) {
                                if (TextUtils.equals(ShareConstant.QQ_PACKAGE_NAME, resolveInfo.activityInfo.packageName)) {
                                    activityName = resolveInfo.activityInfo.name
                                    break
                                }
                            }
                        }
                        val qqIntent = Intent(Intent.ACTION_SEND)
                        qqIntent.type = "text/plain"
                        qqIntent.putExtra(Intent.EXTRA_SUBJECT, data.title)
                        qqIntent.setClassName(ShareConstant.QQ_PACKAGE_NAME, activityName)
                        qqIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                        qqIntent.putExtra(Intent.EXTRA_TEXT, data.content)
                        if (ShareUtil.startActivity(activity, qqIntent)) {
                            shareCallback?.success(channel)
                        } else {
                            shareCallback?.failed(channel, SHARE_STATUS_FAILED, "启动QQ失败")
                        }
                    }
                }
            }
        }
    }

    private var mCallBack = object : IUiListener {
        override fun onComplete(o: Any) {
            shareCallback?.success(channel)
        }

        override fun onError(uiError: UiError?) {
            shareCallback?.failed(channel, SHARE_STATUS_FAILED, uiError?.errorMessage)
        }

        override fun onCancel() {
            shareCallback?.cancel(channel)
        }
    }

}